package com.jcl.recruit.timer.crawler.service.impl;

import com.google.gson.Gson;
import com.jcl.recruit.resume.model.ResumeOrig;
import com.jcl.recruit.searchresume.model.SearchResume;
import com.jcl.recruit.searchresume.model.SearchResumeEdu;
import com.jcl.recruit.searchresume.model.SearchResumeWork;
import com.jcl.recruit.timer.crawler.bean.DajieJsonBean;
import com.jcl.recruit.timer.crawler.bean.JxBean;
import com.jcl.recruit.timer.crawler.bean.JxResumeBean;
import com.jcl.recruit.timer.crawler.service.CrawlerChannelResumeJx;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName crawlerChannelResumeTaskService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/30 15:57
 * @Version 1.0
 **/
public class DajieCrawlerResumeTask implements CrawlerChannelResumeJx {


    @Override
    public JxBean crawlerResume(List<ResumeOrig> resumeOrigList) throws Exception{
        JxBean jxBean = new JxBean();
        List<JxResumeBean> jxResumeBeans = new ArrayList<>();
        jxBean.setJxResumeBeans(jxResumeBeans);

        for(ResumeOrig orig: resumeOrigList) {

            jxBean.setExternalId(orig.getId());
            JxResumeBean jxResumeBean = new JxResumeBean();
            jxResumeBeans.add(jxResumeBean);

            String json = orig.getContent();
            DajieJsonBean dajieJsonBean = new Gson().fromJson(json, DajieJsonBean.class);
            SearchResume searchResume = new SearchResume();
            searchResume.setRealname(dajieJsonBean.getName());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String regexNumber = "[^0-9]";
            if (dajieJsonBean.getAge() != null) {
                Integer birthYear = Integer.valueOf(sdf.format(new Date())) - dajieJsonBean.getAge();
                searchResume.setBirthday(sdf.parse(birthYear.toString(), new ParsePosition(0)));
            }
            if (StringUtils.isNotBlank(dajieJsonBean.getMobile()) && !dajieJsonBean.getMobile().contains("*")) {
                searchResume.setMobile(dajieJsonBean.getMobile());
            }
            if (StringUtils.isNotBlank(dajieJsonBean.getEmail()) && !dajieJsonBean.getEmail().contains("*")) {
                searchResume.setEmail(dajieJsonBean.getEmail());
            }
            searchResume.setCurrentCity(dajieJsonBean.getLiveCity());
            searchResume.setCurrentAddress(dajieJsonBean.getLiveCity());
            if(StringUtils.isNotBlank(dajieJsonBean.getWorkExp())) {
                searchResume.setWorkyear(Integer.valueOf(dajieJsonBean.getWorkExp().replaceAll(regexNumber, "")));
            }
            List<DajieJsonBean.Education> edus = dajieJsonBean.getEducations();
            List<SearchResumeEdu> searchResumeEdus = new ArrayList<>();
            if (edus != null) {
                searchResume.setEduLastSchool(edus.get(0).getSchoolName());
                searchResume.setEduLastMajor(edus.get(0).getMajor());
                searchResume.setEduLastEducation(edus.get(0).getDegree());
                for (DajieJsonBean.Education entity : edus) {
                    SearchResumeEdu searchResumeEdu = new SearchResumeEdu();
                    searchResumeEdu.setStartDate(entity.getStartDate());
                    searchResumeEdu.setEndDate(entity.getEndDate());
                    searchResumeEdu.setMajor(entity.getMajor());
                    searchResumeEdu.setSchool(entity.getSchoolName());
                    searchResumeEdu.setDegree(entity.getDegree());
                    searchResumeEdus.add(searchResumeEdu);
                }
            }
            List<DajieJsonBean.Work> works = dajieJsonBean.getWorkExperiences();
            List<SearchResumeWork> searchResumeWorks = new ArrayList<>();
            if (works != null) {
                searchResume.setWorkLastCompany(works.get(0).getCorpName());
                searchResume.setWorkLastPosition(works.get(0).getPosition());
                searchResume.setWorkDetail(works.get(0).getJobKind());
                for (DajieJsonBean.Work entity : works) {
                    SearchResumeWork searchResumeWork = new SearchResumeWork();
                    searchResumeWork.setStartDate(entity.getStartDate());
                    searchResumeWork.setEndDate(entity.getEndDate());
                    searchResumeWork.setCompany(entity.getCorpName());
                    searchResumeWork.setPosition(entity.getPosition());
                    searchResumeWork.setDuty(entity.getJobKind());
                    searchResumeWorks.add(searchResumeWork);
                }
            }
            searchResume.setSourceSub("dajie");
            searchResume.setApplyStatus(0);
            searchResume.setCreatetime(new Date());
            searchResume.setUpdateDate(searchResume.getCreatetime());
            searchResume.setStatus(0);
            searchResume.setKeyword(orig.getKeyword());

            jxResumeBean.setSearchResume(searchResume);
            jxResumeBean.setResumeWorks(searchResumeWorks);
            jxResumeBean.setSearchResumeEdus(searchResumeEdus);

        }

        return jxBean;
    }
}
